﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PuroGame.Repositories.Repository;
using PuroGame.ViewData.Mercadao;
using PuroGame.Business;
using System.IO;
using PuroGame.Helpers;

namespace MercadaoPuroGame.Web.Controllers
{
    public class AnuncioController : Controller
    {
       
        //
        // GET: /Anuncio/

        public ActionResult Index(int id)
        {
            MercadaoRepository _repositorio = new MercadaoRepository();
            mercadao anuncio = _repositorio.Carregar(id);
            HomeViewData model = new HomeViewData();
            model.ID = anuncio.id;
            model.Titulo = anuncio.titulo;
            model.Preco = anuncio.preco;
            model.Data = anuncio.data;
            model.Texto = anuncio.texto;
            return View(model);
        }

        public ActionResult Cadastrar()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CadastrarAnuncio(HomeViewData model)
        {
            PuroGame.Model.Mercadao.HomeModel homeModel = new PuroGame.Model.Mercadao.HomeModel();
            homeModel.SalvarAnuncio(model);
            return View("Cadastrar", new HomeViewData());
        }

        [HttpPost]
        public ActionResult UploadFiles(IEnumerable<HttpPostedFileBase> files)
        {
            List<string> arquivos = new List<string>();

            foreach (HttpPostedFileBase file in files)
            {
                System.Drawing.Image image = null;
                
                try
                {
                    string SavePath = Server.MapPath(@"\Content\anuncios\temp");
                    image = System.Drawing.Image.FromStream(file.InputStream);
                    SavePath += @"\anuncio_temp" + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + "_ss" +
                        files.ToList().IndexOf(file).ToString();
                    ImageHelper.GerarImagem(image, 640, 0, System.Drawing.Imaging.ImageFormat.Jpeg, SavePath + ".jpg");
                    ImageHelper.GerarImagem(image, 150, 0, System.Drawing.Imaging.ImageFormat.Jpeg, SavePath + "_tumb.jpg");
                    arquivos.Add(SavePath + ".jpg");
                    arquivos.Add(SavePath + "_tumb.jpg");
                }
                catch (Exception ex)
                {
                    Response.Write("Erro ao enviar imagem!");
                }
                finally
                {
                    image.Dispose();
                }
            }

            return Json(arquivos);
        }

        private byte[] ReadData(Stream stream)
        {
            byte[] buffer = new byte[16 * 1024];

            using (MemoryStream ms = new MemoryStream())
            {
                int read;
                while ((read = stream.Read(buffer, 0, buffer.Length)) > 0)
                {
                    ms.Write(buffer, 0, read);
                }

                return ms.ToArray();
            }
        }
    }
}
